package cn.aitrox.ry.app.web.controller.anno;

import cn.aitrox.ry.app.web.config.AppNacosProperties;
import cn.aitrox.ry.app.web.service.AnnoResultService;
import cn.aitrox.ry.app.web.vo.anno.result.in.AnnoResultExportInVo;
import cn.aitrox.ry.app.web.vo.anno.result.out.AnnoResultUsersOutVo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.bean.ExportFileCallback;
import cn.aitrox.ry.common.controller.BaseController;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoResultReadInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoResultSubmitInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoSeriesGetInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoUsersInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoYayInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.DeepzoomUrlInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.out.AnnoResultDto;
import cn.aitrox.ry.service.anno.dto.anno.result.out.DeepzoomUrlOutDto;
import cn.aitrox.ry.service.anno.dto.anno.result.out.SeriesGetResultDto;
import cn.aitrox.ry.util.ExcelUtil;
import cn.aitrox.ry.util.UUIDUtil;
import cn.aitrox.ry.util.ZipUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
public class AnnoResultController extends BaseController {

    private static final List<String> ANNO_RESULT_XLSX_SHEET_NAMES = Arrays.asList("结果", "征象字典");

    @Autowired
    private AppNacosProperties appNacosProperties;

    @Autowired
    private AnnoResultService annoResultService;

    @PostMapping(value = "/anno/result/series/get")
    RespResult<SeriesGetResultDto> seriesGet(@Validated @RequestBody AnnoSeriesGetInDto input) {
        return annoResultService.seriesGet(input);
    }

    @PostMapping(value = "/anno/result/read")
    RespResult<AnnoResultDto> read(@Validated @RequestBody AnnoResultReadInDto input) {
        return annoResultService.read(input);
    }

    @PostMapping(value = "/anno/result/yay")
    RespResult yay(@Validated @RequestBody AnnoYayInDto input) {
        return annoResultService.yay(input);
    }

    @PostMapping(value = "/anno/result/submit")
    RespResult submit(@Validated @RequestBody AnnoResultSubmitInDto input) {
        return annoResultService.submit(input);
    }

    @PostMapping(value = "/anno/result/users")
    RespResult<AnnoResultUsersOutVo> annoUsers(@Validated @RequestBody AnnoUsersInDto input) {
        return annoResultService.annoUsers(input);
    }

    @PostMapping(value = "/anno/result/export")
    public ResponseEntity export(@Validated @RequestBody AnnoResultExportInVo input) throws IOException {
        Map<Integer, List<List<List<String>>>> exportData = annoResultService.export(input.getTaskIds());
        File tmpParentFile = new File(appNacosProperties.getTmpFileParentPath() + UUIDUtil.getUUID());
        String fileName;
        File file;
        File zipParentFile = new File(tmpParentFile, "zip");
        File xlsxParentFile = new File(tmpParentFile, "task");
        zipParentFile.mkdirs();
        xlsxParentFile.mkdirs();

        if (input.getTaskIds().size() > 1) {
            fileName = "结果导出.zip";
            file = new File(zipParentFile, fileName);
        } else {
            fileName = "结果导出.xlsx";
            file = new File(xlsxParentFile, fileName);
        }

        return exportFileUseByte(tmpParentFile, fileName, file, new ExportFileCallback() {
            @Override
            public File writeTmpFile(File exportFile) throws IOException {
                if (input.getTaskIds().size() > 1) {
                    for (Integer taskId : input.getTaskIds()) {
                        File xlsxFile = new File(xlsxParentFile, taskId + ".xlsx");
                        List<List<List<String>>> taskExportData = exportData.get(taskId);
                        ExcelUtil.writeFileMultiSheet(ANNO_RESULT_XLSX_SHEET_NAMES, taskExportData, xlsxFile);
                    }
                    ZipUtil.zipPath(xlsxParentFile.getAbsolutePath(), exportFile);
                } else {
                    ExcelUtil.writeFileMultiSheet(ANNO_RESULT_XLSX_SHEET_NAMES, exportData.get(input.getTaskIds().get(0)), exportFile);
                }
                return exportFile;
            }
        });
    }

    @PostMapping(value = "/anno/result/deepzoom/url")
    public RespResult<DeepzoomUrlOutDto> deepzoomUrl(@Validated @RequestBody DeepzoomUrlInDto input) {
        return annoResultService.deepzoomUrl(input);
    }

}
